package com.example.wxback.controller.schedule;

import com.example.wxback.service.UserMessageDetailService;
import com.example.wxback.service.UserMessageService;
import com.example.wxback.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class MessageScheduledService {

    @Autowired
    private UserMessageService userMessageService;

    @Autowired
    private UserMessageDetailService userMessageDetailService;

    // 每天凌晨2点执行任务
    @Scheduled(cron = "0 0 2 * * ?")
//    @Scheduled(cron = "0 0/2 * * * ?")
    public void scheduled() {
        log.info("=====>>>>>使用cron  {}", DateUtil.getCurrentTime());
        Date beforeTwoDate = DateUtil.plusDay(new Date(), -2);
        userMessageDetailService.deleteExpiredMessage(beforeTwoDate);

        int countList = userMessageService.deleteExpiredMessage(beforeTwoDate);
        int countDetail = userMessageDetailService.deleteExpiredMessage(beforeTwoDate);
        if (countList > 0) {
            log.info(String.format("本次删除消息列表消息：%s条", countList));
        } else {
            log.info("当前消息列表没有过期消息");
        }

        if (countDetail > 0) {
            log.info(String.format("本次删除消息详情消息：%s条", countDetail));
        } else {
            log.info("当前消息详情没有过期消息");
        }
    }
}
